package com.wetao.module_pen.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.tstudy.blepenlib.data.BleDevice;
import com.wetao.lib_common.utils.MultiClickUtil;
import com.wetao.lib_common.utils.StringUtil;
import com.wetao.module_pen.R;
import com.wetao.module_pen.R2;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PenMainListAdapter extends RecyclerView.Adapter{
    private List<BleDevice> mData = new ArrayList<>();
    private String mLastDevMac = null;

    public PenMainListAdapter() {
    }

    public void setmLastDevMac(String dev) {
        if(StringUtil.isEmpty(dev)) {
            return;
        }
        mLastDevMac = dev;
        for(int i = 0; i < mData.size(); i++){
            if(mLastDevMac.equals(mData.get(i).getMac())) {
                BleDevice item = mData.get(i);
                mData.remove(i);
                addDevice(item);
            }
        }
    }

    public void addDevice(BleDevice dev) {
        if(mLastDevMac != null && dev.getMac().equals(mLastDevMac)) {
            mData.add(0, dev);
        } else {
            mData.add(dev);
        }

        notifyDataSetChanged();
    }

    public void clearDevice() {
        mData.clear();
        notifyDataSetChanged();
    }

    public BleDevice getBleDevice(int position) {
        return mData.get(position);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType == 1) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.pen_main_item_list, parent, false);
            return new MacHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof MacHolder) {
            ((MacHolder)holder).bindData(position);
        }
    }

    @Override
    public int getItemCount() {
        if(mData == null) {
            return 0;
        }
        return mData.size();
    }

    @Override
    public int getItemViewType(int position) {
        return 1;
    }


    class MacHolder extends RecyclerView.ViewHolder{

        @BindView(R2.id.tv_mac)
        TextView tv_mac;
        @BindView(R2.id.tv_last)
        TextView tv_last;


        public MacHolder( View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void bindData(int position) {
            clickListener(position);
            String data = mData.get(position).getMac();
            tv_mac.setText("MAC: " + data);
            if(mLastDevMac != null && mData.get(position).getMac().equals(mLastDevMac)) {
                tv_last.setVisibility(View.VISIBLE);
            } else {
                tv_last.setVisibility(View.GONE);
            }
        }
        private void clickListener(int position) {
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null && MultiClickUtil.isFastClick()) {
                        listener.onItemClick(position);
                    }
                }
            });
        }
    }

    //第一步 定义接口
    public interface OnItemClickListener {
        void onItemClick(int position);
    }
    private OnItemClickListener listener;
    //第二步， 写一个公共的方法
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

}
